package com.syqc.monitor.ui.set

import android.view.KeyEvent
import android.widget.TextView
import androidx.core.view.isGone
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.DraggableModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.MenuKey
import com.syqc.comlib.UserConstant
import com.syqc.comlib.base.BaseBarActivity
import com.syqc.comlib.entity.AppGpsColumn
import com.syqc.comlib.rx.filterFast
import com.syqc.comlib.utils.CacheMMKV
import com.syqc.comlib.utils.GsonUtil
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.comlib.utils.sub
import com.syqc.comlib.utils.toast
import com.syqc.entity.FunInfo
import com.syqc.entity.LocationInfo
import com.syqc.login.FunKey
import com.syqc.login.InfoMode
import com.syqc.login.InfoSetModel
import com.syqc.login.LocationStatusModel
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivityInfoSet2Binding
import com.syqc.utils.AppConfigUtil
import com.syqc.utils.Keys

class InfoSet2Activity :
    BaseBarActivity<ActivityInfoSet2Binding>(ActivityInfoSet2Binding::inflate) {
    private val mFunAdapter by lazy { FunAdapter() }
    private val mInfoAdapter by lazy { InfoSetAdapter() }
    private val mLocationAdapter by lazy { LocationAdapter() }

    private var isDefault = false
        get() = CacheMMKV.decodeBoolean("isDefault", false)
        set(value) {
            CacheMMKV.encode("isDefault", value)
            field = value
        }
    private var isModify=false
        get() = CacheMMKV.decodeBoolean("isModify", false)
        set(value) {
            CacheMMKV.encode("isModify", value)
            field = value
        }
    private var isMonitor=false
        get() = CacheMMKV.decodeBoolean("isMonitor", false)
        set(value) {
            CacheMMKV.encode("isMonitor", value)
            field = value
        }

    //是否发生改变
    private var isChange = false
    private val isCarUser = UserConstant.loginType == Keys.LOGIN_TYPE_CAR
    override fun initView() {
        ToolbarHelper.setMToolbar(this, R.string.desc_location_info_set).setBackClick {
            if (isChange) {
                if (!isCarUser) InfoSetModel.saveChoose(mFunAdapter.data)
                saveInfo()
            }
            finish()
        }

        binding.llMenuSet.isGone = isCarUser
        binding.rvFunction.isGone = isCarUser
        if (!isCarUser) {
            binding.rvFunction.layoutManager = GridLayoutManager(this, 4)
            binding.rvFunction.adapter = mFunAdapter
            mFunAdapter.draggableModule.isDragEnabled = true

            mFunAdapter.setList(InfoSetModel.getAllInfos())
        }

        binding.rvInfo.layoutManager = GridLayoutManager(this, 3)
        binding.rvInfo.adapter = mInfoAdapter
        mInfoAdapter.draggableModule.isDragEnabled = true
        mInfoAdapter.setList(LocationStatusModel.changeList)

        binding.rv808Info.layoutManager = GridLayoutManager(this, 3)
        binding.rv808Info.adapter = mLocationAdapter
        mLocationAdapter.draggableModule.isDragEnabled = true
        mLocationAdapter.setList(LocationStatusModel.getLocationInfos())

        binding.tvDefault.isSelected=isDefault&& LocationStatusModel.isModelOK(InfoMode.DefaultMode)
        binding.tvModify.isSelected=isModify&&LocationStatusModel.isModelOK(InfoMode.ModifyMode)
        binding.tvMonitor.isSelected=isMonitor&&LocationStatusModel.isModelOK(InfoMode.MonitorMode)
    }

    override fun setListener() {
        binding.tvDefault.filterFast {
            binding.tvDefault.isSelected = true
            binding.tvModify.isSelected = false
            binding.tvMonitor.isSelected = false
            isDefault=true
            isModify=false
            isMonitor=false
            isChange = true
            handleModel(InfoMode.DefaultMode)
        }

        binding.tvModify.filterFast {
            binding.tvDefault.isSelected = false
            binding.tvModify.isSelected = true
            binding.tvMonitor.isSelected = false
            isDefault=false
            isModify=true
            isMonitor=false
            isChange = true
            handleModel(InfoMode.ModifyMode)
        }

        binding.tvMonitor.filterFast {
            binding.tvDefault.isSelected = false
            binding.tvModify.isSelected = false
            binding.tvMonitor.isSelected = true
            isDefault=false
            isModify=false
            isMonitor=true
            isChange = true
            handleModel(InfoMode.MonitorMode)
        }

        mFunAdapter.setOnItemClickListener { adapter, _, position ->
            binding.tvDefault.isSelected = false
            binding.tvModify.isSelected = false
            binding.tvMonitor.isSelected = false
            isDefault=false
            isModify=false
            isMonitor=false
            val funInfo = mFunAdapter.data[position]
            if (funInfo.isChoose) {
                funInfo.isChoose = false
                InfoSetModel.chooseNum--
            } else {
                if (InfoSetModel.chooseNum > 11) {
                    R.string.toast_choose_over.toast(this)
                    return@setOnItemClickListener
                }
                funInfo.isChoose = true
                InfoSetModel.chooseNum++
            }
            isChange = true
            adapter.notifyItemChanged(position)

        }
        mFunAdapter.setOnItemLongClickListener { _, _, _ ->
            isChange = true
            return@setOnItemLongClickListener false
        }

        mInfoAdapter.setOnItemClickListener { adapter, _, position ->
            val funInfo = mInfoAdapter.data[position]
            funInfo.switch = if (funInfo.switch == 0) 1 else 0
            adapter.notifyItemChanged(position)
            isChange = true
            binding.tvDefault.isSelected = false
            binding.tvModify.isSelected = false
            binding.tvMonitor.isSelected = false
            isDefault=false
            isModify=false
            isMonitor=false
        }

        mInfoAdapter.setOnItemLongClickListener { _, _, _ ->
            isChange = true
            return@setOnItemLongClickListener false
        }

        mLocationAdapter.setOnItemClickListener { adapter, _, position ->
            val locationInfo = mLocationAdapter.data[position]
            locationInfo.switch = if (locationInfo.switch == 0) 1 else 0
            adapter.notifyItemChanged(position)
            isChange = true
        }
        mLocationAdapter.setOnItemLongClickListener { _, _, _ ->
            isChange = true
            return@setOnItemLongClickListener false
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && isChange) {
            if (!isCarUser) InfoSetModel.saveChoose(mFunAdapter.data)
            saveInfo()
            finish()
            return false
        }
        return super.onKeyDown(keyCode, event)
    }

    private fun saveInfo() {
        LocationStatusModel.update(mInfoAdapter.data)
        LocationStatusModel.saveLocal()
        if (UserConstant.loginType == Keys.LOGIN_TYPE_CAR) {
            AppConfigUtil.saveCarConfig(
                UserConstant.current_carId ?: "",
                GsonUtil.toJson(LocationStatusModel.changeList)
            )
        } else {
            AppConfigUtil.saveConfig(
                3,
                GsonUtil.toJson(LocationStatusModel.changeList)
            )
        }
        LocationStatusModel.saveLocations(mLocationAdapter.data)
    }

    //处理模式
    private fun handleModel(model: InfoMode) {
        InfoSetModel.changModel(model)
        LocationStatusModel.changeMode(model)
        mInfoAdapter.setList(LocationStatusModel.changeList)
        mFunAdapter.setList(InfoSetModel.getAllInfos())
    }

}

class FunAdapter : BaseQuickAdapter<FunInfo, BaseViewHolder>(R.layout.item_fun_info),
    DraggableModule {
    override fun convert(holder: BaseViewHolder, item: FunInfo) {
        val title = holder.getView<TextView>(R.id.tvTitle)
        title.isSelected = item.isChoose
        title.setText(getTextById(item.key))
    }

    //
    private fun getTextById(key: String): Int {
        return when (key) {
            MenuKey.MENU_106 -> R.string.fun_name_0
            FunKey.FUN_Talk -> R.string.fun_name_2
            MenuKey.MENU_104 -> R.string.fun_name_3
            MenuKey.MENU_917 -> R.string.fun_name_13
            MenuKey.MENU_229 -> R.string.fun_name_9
            MenuKey.MENU_201 -> R.string.fun_name_6
            MenuKey.MENU_912 -> R.string.fun_name_10
            MenuKey.MENU_916 -> R.string.fun_name_5
            MenuKey.MENU_103 -> R.string.fun_name_4
            MenuKey.MENU_204 -> R.string.fun_name_7
            MenuKey.MENU_902 -> R.string.fun_name_8
            MenuKey.MENU_907 -> R.string.fun_name_11
            MenuKey.MENU_908 -> R.string.fun_name_12
            MenuKey.MENU_903 -> R.string.fun_name_15
            FunKey.FUN_NAVI -> R.string.fun_name_14
            FunKey.FUN_CALL -> R.string.fun_name_16
            else -> R.string.desc_other
        }
    }
}

class InfoSetAdapter :
    BaseQuickAdapter<AppGpsColumn, BaseViewHolder>(R.layout.item_fun_info),
    DraggableModule {
    override fun convert(holder: BaseViewHolder, item: AppGpsColumn) {
        val title = holder.getView<TextView>(R.id.tvTitle)
        title.isSelected = item.switch == 1
        title.text = context.getString(LocationStatusModel.getDescByKey(item.key))
    }
}

class LocationAdapter : BaseQuickAdapter<LocationInfo, BaseViewHolder>(R.layout.item_fun_info),
    DraggableModule {
    override fun convert(holder: BaseViewHolder, item: LocationInfo) {
        val title = holder.getView<TextView>(R.id.tvTitle)
        title.isSelected = item.switch == 1
        val name = item.name
        title.text = if (name.length > 6) sub(name, 5) else name
    }
}